package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageResult result = service.query(qo);
        model.addAttribute("result", result);
        return "department/list";
    }
    @RequiredPermission({"部门编辑","department:input"})
    @RequestMapping("input")
    public String input(Model model,Long id){
        if (id != null){
            Department department = service.get(id);
            model.addAttribute("dept",department);
        }
        return "department/input";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"部门修改","department:saveOrUpdate"})
    public  String saveOrUpdate(Department department){
        service.saveOrUpdate(department);
        return "redirect:/dept/list.do";
    }
    @RequestMapping("/delete")
    @RequiredPermission({"部门删除","department:delete"})
    public  String delete(Long id){
        service.delete(id);
        return "redirect:/dept/list.do";
    }
}
